<html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>教师后台-系部管理</title>
    <style>
        /* .container {
            min-height: 100vh;
        } */

        /* .container .nav {
            height: 100vh;
            background-color: #fff;
        } */

        .container .leftmenu {
            min-height: 100vh;
            background-color: #fff;
        }

        .link-secondary {
            text-decoration: none;
        }

        .link-secondary:hover {
            color: #cd70e2 !important;
        }
        td img{
            max-width: 120px;
        }
    </style>
</head>
<body>
    <!-- container是bootstrap的居中窄的显示方案 -->
    <div class="container bg-body-secondary">
        <div class="row">
            <div class="col-md-2 leftmenu" id="leftmenu"></div>
            <div class="col-md-9 col-xs-12 kc ms-3">
                <div class="row mt-3 pb-1 border-bottom border-info">
                    <div class="col-md-2">系部列表</div>
                    <div class="col-md-2 offset-md-8">
                        <a class="nav-link link-success" href="./depart_creat.html">
                            <i class="bi bi-plus-circle-fill"></i>
                            添加系部
                        </a>
                    </div>
                </div>

                <div class="row mt-3">
                    <table class="table table-bordered">
                        <thead>
                            <th>序号</th>
                            <th>系部名称</th>
                            <th>系部编号码</th>
                            <th>学校编号码</th>
                            <th>创建时间</th>
                            <th>操作</th>
                        </thead>
                        <tbody id="tbody"></tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
    <script type="module" src="../js/main.js"></script>
    <script src="../js/jquery.min.js"></script>
    <script src="../js/common.js"></script>
    <script>
        $(document).ready(function () {
            $.get("http://unicloud.codecore.cn/ms/departs", {}, function (params) {
                if (params.errCode == 0) {
                    let data = params.data;
                    for (let i = 0; i < data.length; i++) {
                        let elem = data[i]
                        $("#tbody").append($(`<tr>
                                <td>${i+1}</td>
                                <td>${elem.department_name}</td>
                                <td>${elem.department_code}</td>
                                <td>${elem.school_code}</td>
                                <td>${elem.create_time}</td>
                                <td>
                                    <button class="btn btn-sm btn-success" data-id="${elem._id}">编辑</button>
                                    <button class="btn btn-sm btn-danger" data-id="${elem._id}">删除</button>
                                </td>
                            </tr>`))
                    }
                }
            })
            $("#tbody").delegate("button.btn-success", "click", function () {
                let data = $(this).data();
                location.href = `./depart_edit.html?id=${data.id}`
            });
            $("#tbody").delegate("button.btn-danger", "click", function () {
                let data = $(this).data();
                $.post("http://unicloud.codecore.cn/ms/departRemove",data,function(params) {
                    console.log(params);
                    if(params.errCode == 0){
                        location.reload()
                    }else{
                        alert("删除失败")
                    }
                })
            });
        })
    </script>
</body>
</html>